﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using System.Configuration;

namespace ReportForm 
{
    public partial class frmInventory : UTLBASEFORM.MDICHILD
    {
        private static string server = ConfigurationManager.AppSettings["Server"];
        private static string User = ConfigurationManager.AppSettings["UserID"];
        private static string Pass = ConfigurationManager.AppSettings["Password"];
        private static string Database = ConfigurationManager.AppSettings["DataBase"];

        public frmInventory()
        {
            InitializeComponent();
            SetupMenuBar();
            ShowPanel(1);
        }

        private void SetupMenuBar()
        {
            ShowBTN1("Báo Cáo Nhập",null);
            ShowBTN2("Báo Cáo Xuất", null);
            //ShowBTN3("Báo Cáo Tồn", null);
        }
        private void ShowPanel(int id ) {
            if (id == 1)
            {
                panel1.Visible = true;
                panel2.Visible = false;
            }
            else {
                panel1.Visible = false;
                panel2.Visible = true;
            }


        }
        private void LoadCat() {
            Treport tReport = new Treport();
            DataTable dt = tReport.GetCat();
            cmbCat.DataSource = dt;
            cmbCat.DisplayMember="Name";
            cmbCat.ValueMember ="ID";
        }
        private void frmInventory_Load(object sender, EventArgs e)
        {
            SetupMenuBar();
        }
        public override void btn1_Click(object sender, EventArgs e)
        {
            ShowPanel(1);
        }
        public override void btn2_Click(object sender, EventArgs e)
        {
            ShowPanel(2);
            LoadCat();
        }
        public void ShowReportBuy(string to , string from) {

            string path = Application.StartupPath;
            ReportDocument reportDocument = new ReportDocument();
            ParameterFields paramFields = new ParameterFields();

            ParameterField paramDateFrom = new ParameterField();
            ParameterDiscreteValue paramValueFrom = new ParameterDiscreteValue();
            ParameterField paramDateTo = new ParameterField();
            ParameterDiscreteValue paramValueTo = new ParameterDiscreteValue();


            //Set instances for input parameter 1 -  @vDepartment
            paramDateFrom.Name = "@DateFrom";
            //Below variable can be set to any data present in SalseData table, Department column
            paramValueFrom.Value = DateTime.Parse(from);
            paramDateFrom.CurrentValues.Add(paramValueFrom);
            //Add the paramField to paramFields
            paramFields.Add(paramDateFrom);

            paramDateTo.Name = "@DateTo";

            //Below variable can be set to any data present in SalseData table, Department column
            paramValueTo.Value = DateTime.Parse(to);
            paramDateTo.CurrentValues.Add(paramValueTo);
            //Add the paramField to paramFields
            paramFields.Add(paramDateTo);

            crystalReportViewer1.ParameterFieldInfo = paramFields;
               // ReportDAO rpt = new ReportDAO();
            reportDocument.Load(path + "\\rpBuy.rpt");
            //  rpt.kenoi(reportDocument);

            reportDocument.SetDatabaseLogon(User, Pass, server, Database, true);
            TableLogOnInfo logonInfo = new TableLogOnInfo();
            foreach (Table table in reportDocument.Database.Tables)
            {
                logonInfo = table.LogOnInfo;
                logonInfo.ConnectionInfo.ServerName = server;
                logonInfo.ConnectionInfo.UserID = User;
                logonInfo.ConnectionInfo.Password = Pass;
                logonInfo.ConnectionInfo.DatabaseName = Database;
                table.ApplyLogOnInfo(logonInfo);
            }
            //set the database loggon information.
            //**Note that the third parameter is the DSN name and not the Database or System name

            //  reportDocument.SetDatabaseLogon(User, Pass, server, Database, true);

            // reportDocument.SetDatabaseLogon("sa", "tqtrang07", "TQTRANG", "hongphucgoldDB", false);
            //Load the report by setting the report source

            crystalReportViewer1.ReportSource = reportDocument;
        }
        
        public void ShowReportBuySell( int idCat,string to, string from)
        {

            string path = Application.StartupPath;
            ReportDocument reportDocument = new ReportDocument();
            ParameterFields paramFields = new ParameterFields();
            
            ParameterField paramidCat = new ParameterField();
            ParameterDiscreteValue paramValueidCat = new ParameterDiscreteValue();
            ParameterField paramDateFrom = new ParameterField();
            ParameterDiscreteValue paramValueFrom = new ParameterDiscreteValue();
            ParameterField paramDateTo = new ParameterField();
            ParameterDiscreteValue paramValueTo = new ParameterDiscreteValue();

            //Set instances for input parameter 1 -  @vDepartment
            paramidCat.Name = "@idcat";
            //Below variable can be set to any data present in SalseData table, Department column
            paramValueidCat.Value = idCat;
            paramidCat.CurrentValues.Add(paramValueidCat);
            //Add the paramField to paramFields
            paramFields.Add(paramidCat);

            //Set instances for input parameter 1 -  @vDepartment
            paramDateFrom.Name = "@tungay";
            //Below variable can be set to any data present in SalseData table, Department column
            paramValueFrom.Value = DateTime.Parse(from);
            paramDateFrom.CurrentValues.Add(paramValueFrom);
            //Add the paramField to paramFields
            paramFields.Add(paramDateFrom);

            paramDateTo.Name = "@denngay";

            //Below variable can be set to any data present in SalseData table, Department column
            paramValueTo.Value = DateTime.Parse(to);
            paramDateTo.CurrentValues.Add(paramValueTo);
            //Add the paramField to paramFields
            paramFields.Add(paramDateTo);

            crystalReportViewer1.ParameterFieldInfo = paramFields;
            // ReportDAO rpt = new ReportDAO();
            //reportDocument.Load("BuySell.rpt");
            reportDocument.Load(path + "\\BuySell.rpt");
            //  rpt.kenoi(reportDocument);

            reportDocument.SetDatabaseLogon(User, Pass, server, Database, true);
            TableLogOnInfo logonInfo = new TableLogOnInfo();
            foreach (Table table in reportDocument.Database.Tables)
            {
                logonInfo = table.LogOnInfo;
                logonInfo.ConnectionInfo.ServerName = server;
                logonInfo.ConnectionInfo.UserID = User;
                logonInfo.ConnectionInfo.Password = Pass;
                logonInfo.ConnectionInfo.DatabaseName = Database;
                table.ApplyLogOnInfo(logonInfo);
            }
            //set the database loggon information.
            //**Note that the third parameter is the DSN name and not the Database or System name

            //  reportDocument.SetDatabaseLogon(User, Pass, server, Database, true);

            // reportDocument.SetDatabaseLogon("sa", "tqtrang07", "TQTRANG", "hongphucgoldDB", false);
            //Load the report by setting the report source

            crystalReportViewer1.ReportSource = reportDocument;
        }
        private void cmdBuyReport_Click(object sender, EventArgs e)
        {

            ShowReportBuy(dtkTo.Value.ToShortDateString(), dtkFrom.Value.ToShortDateString());
        }

        private void cmdReportBuySell_Click(object sender, EventArgs e)
        {
            MessageBox.Show(cmbCat.SelectedValue.ToString());
            MessageBox.Show(cmbCat.SelectedValue.ToString());
            MessageBox.Show(cmbCat.SelectedValue.ToString());
            ShowReportBuySell(int.Parse(cmbCat.SelectedValue.ToString()), dpkFromBuySell.Value.ToShortDateString(), dpkToBuySell.Value.ToShortDateString());

        }

     
    }
}
